package recommendation.contentBased;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import data.ItemProfile;
import data.KeywordVector;
import data.UserProfile;


public class RecommendationManager {
	private static RecommendationManager managerInstance = null;
	private ArrayList<UserProfile> userProfiles;
	private ArrayList<ItemProfile> itemProfiles;
	
	public static RecommendationManager getInstance(){
		if(managerInstance == null){
			managerInstance = new RecommendationManager();
		}
		return managerInstance;
	}
	
	private RecommendationManager() {
		this.itemProfiles = new ArrayList<ItemProfile>();
	}
	
	public void fillItems(ArrayList<KeywordVector> itemKeywords){
		Iterator<KeywordVector> keywordItr = itemKeywords.iterator();
		while(keywordItr.hasNext()){
			KeywordVector itemKeywordVector = keywordItr.next();
			itemProfiles.add(new ItemProfile(itemKeywordVector));
		}
	}
	
	public Map<Long, Double> contentBasedRecommend(UserProfile userProfile){
		Map<Long, Double> recommendationMap = new HashMap<Long, Double>();
		Iterator<ItemProfile> itemItr = this.itemProfiles.iterator();
		while(itemItr.hasNext()){
			ItemProfile itemProfile = itemItr.next();
			double userItemSimilariy = userProfile.getItemSimilarity(itemProfile);
			recommendationMap.put(itemProfile.getID(), userItemSimilariy);
//			System.out.println("ItemID: " + itemProfile.getID() + " -  Similarity: " + userItemSimilariy);
		}
		//TODO sort recommendation map
		return recommendationMap;
	}
	
	public ItemProfile getItemProfile(long vectorID){
		Iterator<ItemProfile> itemProfileItr = this.itemProfiles.iterator();
		while(itemProfileItr.hasNext()){
			ItemProfile itemProfile = itemProfileItr.next();
			if(itemProfile.getID() == vectorID){
				return itemProfile;
			}
		}
		return null;
	}
}
